#include <QCoreApplication>

#include "QHttpClient.h"

using namespace QtNet;

class CString
{
public:
    void operator()(QString v, QNetworkReply *reply) const {
        qDebug() << "QString operator():" << v;
    }

    void call() {
        QNetworkRequest request;
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/text");
        request.setUrl(QUrl("http://192.168.2.122:8081/text"));

        QHttpClient::get(request, this, &CString::String);

    }

    void String(QString v, QNetworkReply *reply) {
        qDebug() << "QString CString func:" << v;
    }
};

void String(QString v, QNetworkReply *reply)
{
    qDebug() << "QString func:" << v;
}

// 普通成员函数 QString
void test_get_string_member_function()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/text");
    request.setUrl(QUrl("http://192.168.2.122:8081/text"));

    QHttpClient::get(request, String);
}

// lambda QString
void test_get_string_lambda()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/text");
    request.setUrl(QUrl("http://192.168.2.122:8081/text"));

    QHttpClient::get(request, [](QString v, QNetworkReply *reply) {
        qDebug() << "QString lambda:" << v;
    });
}

// 类函数对象 QString
void test_get_string_functor()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/text");
    request.setUrl(QUrl("http://192.168.2.122:8081/text"));

    QHttpClient::get(request, CString());
}

// 类成员函数 QString
void test_get_string_member_callback()
{
    CString ccstring;
    ccstring.call();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    test_get_string_member_function();
    test_get_string_lambda();
    test_get_string_functor();
    test_get_string_member_callback();

    return a.exec();
}
